const app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    info:{},
    promoter_shop_name:'',
    background_img:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    app.sendRequest({
      url:'System.Distribution.promoterDetail',
      data:{},
      success:function(res){
        let info = res.data;
        info.background_img = app.IMG(info.background_img);
        that.setData({
          info: info
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 店铺名称改变
   */
  changeName:function(e){
    let that = this;
    let promoter_shop_name = e.detail.value;
    that.setData({
      promoter_shop_name: promoter_shop_name
    })
  },

  /**
   * 上传图片
   */
  uploadImg: function (e) {
    let that = this;
 
    //选择图片
    wx.chooseImage({
      count: 1,
      sizeType: 'compressed',
      success: function (res) {
        let file_Path = res.tempFilePaths[0];
        let tempFiles = res.tempFiles[0];

        //上传至服务器
        that.uplodeHeadImg(that, file_Path);
      },
      fail: function (res) {
        app.showBox(that, '无法获取本地图片');
      }
    })
  },

  /**
   * 上传至服务器
   */
  uplodeHeadImg: function (that, file_Path) {

    let base = app.globalData.siteBaseUrl;

    if (file_Path == '') {
      wx.navigateBack({
        delta: 1
      })
    }

    let param = {
      token: app.globalData.token,
      file_path: 'fxshop',
      method: 'System.Upload.uploadImage'
    };
    param.sign = app.encodeSign(param);
    param.private_key = app.globalData.private_key;

    wx.uploadFile({
      url: base + 'api.php?method=System.Upload.uploadImage',
      filePath: file_Path,
      name: 'img_upload',
      formData: param,
      success: function (res) {

        let data = res.data;

        try {
          data = JSON.parse(JSON.parse(data));
        } catch (e) {
          app.showBox(that, '上传失败');
        }

        let code = data.code;
        if (code == 0) {
          data = data.data;
          let code = data.code;
          let message = data.message;
          let img_url = data.data;
          img_url = app.IMG(img_url);

          if (code > 0) {
            that.setData({
              background_img: img_url
            })
          } else {
            app.showBox(that, message);
          }

        } else {
          app.showBox(that, '上传失败');
        }
      },
      fail: function (res) {
        app.showBox(that, '上传失败');
      }
    })
  },

  /**
   * 保存图片
   */
  saveBtn:function(){
    let that = this;
    let promoter_id = that.data.info.promoter_id;
    let promoter_shop_name = that.data.promoter_shop_name;
    if (promoter_shop_name == ''){
      promoter_shop_name = that.data.info.promoter_shop_name;
    }
    let background_img = that.data.background_img;
    app.sendRequest({
      url:'System.Distribution.updatePromoter',
      data:{
        promoter_id: promoter_id,
        promoter_shop_name: promoter_shop_name,
        background_img: background_img
      },
      success:function(res){
        if(res.data > 0){
          wx.navigateTo({
            url: '/pagesnfx/pages/distribution/distributioncenter/distributioncenter',
          })
        }
      }
    })
  }


})